import { router } from '../routes/routes';
import user from '../servers/user';
import utils from '../../public/utils';
import { Request, Response } from 'express';

/**
 * 验证登录token，并添加自定义回调函数
 * @param callback 回调函数
 * @returns 返回express中间件的回调函数
 */
const addCustomCallback = (callback: Function) => {
  return async (req: Request, res: Response) => {
    if (!(await utils.verifyLoginStatus(req.headers))) {
      res.status(400).send({
        code: 400,
        msg: 'token异常，请重新登录',
        data: void 0
      });
      return;
    }
    callback(req, res);
  };
};

router.post('/login', async (req, res) => {
  if (!utils.verifyHeaderParam(req.headers, 'x-device-id', 'string')) {
    res.status(400).send({
      code: 400,
      msg: '无法获取客户端ID',
      data: void 0
    });
    return;
  }
  const clientId: string = req.headers['x-device-id'] as string;
  if (
    !req.body.hasOwnProperty('username') &&
    !req.body.hasOwnProperty('password')
  ) {
    res.status(400).send({
      code: 400,
      msg: '请输入账号密码',
      data: void 0
    });
    return;
  }
  const { username, password } = req.body;
  //判断账号和密码是否合法，并且判断是否为字符串
  if (
    !username ||
    !password ||
    typeof username !== 'string' ||
    typeof password !== 'string'
  ) {
    res.status(400).send({
      code: 400,
      msg: '参数格式错误',
      data: void 0
    });
    return;
  }
  const result = await user.login(username, password, clientId);
  res.status(result.code).send(result);
});

router.post('/register', async (req, res) => {
  if (!utils.verifyRequestParam(req.body, 'username', 'string')) {
    res.status(400).send({
      code: 400,
      msg: '用户名无效，请检查是否填写正确',
      data: void 0
    });
    return;
  }
  if (!utils.verifyRequestParam(req.body, 'password', 'string')) {
    res.status(400).send({
      code: 400,
      msg: '用户名无效，请检查是否填写正确',
      data: void 0
    });
    return;
  }
  if (!utils.verifyRequestParam(req.body, 'name', 'string')) {
    res.status(400).send({
      code: 400,
      msg: '昵称无效，请检查是否填写正确',
      data: void 0
    });
    return;
  }
  const result = await user.register(
    req.body.username,
    req.body.password,
    req.body.name
  );
  res.status(result!.code).send(result);
});

router.get(
  '/searchUser',
  addCustomCallback(async (req: Request, res: Response) => {
    if (!utils.verifyRequestParam(req.query, 'keyWord', 'string')) {
      res.status(400).send({
        code: 400,
        msg: '请求参数错误，请检查参数是否完整或正确',
        data: void 0
      });
      return;
    }
    const keyWord: string = req.query.keyWord as string; //获取搜索关键词
    const token = req.headers['authorization'] as string;
    const userInfo = await utils.getUserInfo(token);
    if (!userInfo) {
      res
        .status(400)
        .send({ code: 400, msg: '无法获取用户信息', data: void 0 });
      return;
    }
    const result = await user.searchUser(userInfo.id, keyWord);

    res.status(result!.code).send(result);
  })
);

router.post('/addFriend', async (req, res) => {
  if (!(await utils.verifyLoginStatus(req.headers))) {
    res.status(400).send({
      code: 400,
      msg: '请求头参数异常，请重新登录',
      data: void 0
    });
    return;
  }
  if (!utils.verifyRequestParam(req.body, 'friendId', 'string')) {
    res.status(400).send({
      code: 400,
      msg: '请求参数错误，请检查参数是否完整或正确',
      data: void 0
    });
    return;
  }
  const token = req.headers['authorization'] as string;
  const userInfo = await utils.getUserInfo(token);
  if (!userInfo) {
    res.status(400).send({ code: 400, msg: '无法获取用户信息', data: void 0 });
    return;
  }
  const result = await user.addFriend(userInfo.id, req.body.friendId);
  res.status(result!.code).send(result);
});

router.get(
  '/getContactList',
  addCustomCallback(async (req: Request, res: Response) => {
    if (!req.query) {
      res.status(400).send({
        code: 400,
        msg: '请传入请求参数',
        data: void 0
      });
      return;
    }
    if (
      !req.query.hasOwnProperty('page') ||
      !req.query.hasOwnProperty('size')
    ) {
      res.status(400).send({
        code: 400,
        msg: '请求参数错误，请检查参数是否完整或正确',
        data: void 0
      });
      return;
    }
    let keyWord = undefined;
    if (req.query.hasOwnProperty('keyWord')) {
      keyWord = req.query.keyWord as string;
    }
    const token = req.headers['authorization'] as string;
    const userInfo = await utils.getUserInfo(token);
    if (!userInfo) {
      res
        .status(400)
        .send({ code: 400, msg: '无法获取用户信息', data: void 0 });
      return;
    }
    const { page, size } = req.query as any as { page: number; size: number };
    const result = await user.getContactList(page, size, userInfo.id, keyWord);
    res.status(result!.code).send(result);
  })
);

router.get(
  '/getSystemMessage',
  addCustomCallback(async (req: Request, res: Response) => {
    const token = req.headers['authorization'] as string;
    const userInfo = await utils.getUserInfo(token);
    if (!userInfo) {
      res
        .status(400)
        .send({ code: 400, msg: '无法获取用户信息', data: void 0 });
      return;
    }
    const result = await user.getSystemMessage(userInfo.id);
    res.status(result!.code).send(result);
  })
);